package com.lc.service.impl;

import com.guigu.mapper.*;
import com.guigu.pojo.*;
import com.lc.service.MenuService;
import com.lc.vo.TmenuView;
import com.lc.vo.assessView;
import com.lc.vo.workbenchView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class MenuServiceimpl implements MenuService {

    @Autowired
    private TMenuMapper mapper;

    @Resource
    private TOrderMapper tOrderMapper;

    @Resource
    TMemberStatisticsMapper tMemberStatisticsMapper;

    @Resource
    private TMemberMapper tMemberMapper;

    @Resource
    private TInterposeFollowupVisitMapper tInterposeFollowupVisitMapper;

    @Resource
    private TInterposeSchemeMapper tInterposeSchemeMapper;

    @Resource
    private TMentalityResultMapper tMentalityResultMapper;

    @Resource
    private TBodyTypeMapper tBodyTypeMapper;





    @Override
    public List<TmenuView> selectByuseridshowmenu2(int userid) {
        List<TmenuView> Mlist=new ArrayList<TmenuView>();
        List<TMenu> tMenus = this.mapper.selectByuseridshowmenu(userid);
        for(int i=0;i<tMenus.size();i++){
              TmenuView t=new TmenuView();
            List<TMenu> tlist=new ArrayList<TMenu>();
            TMenu tMenu1 = tMenus.get(i);
            if(tMenu1.getParentmenuid()==0){
              for(int j=0;j<tMenus.size();j++){
                  TMenu tMenu2 = tMenus.get(j);
                  if(tMenu2.getParentmenuid()==tMenu1.getId()){
                      tlist.add(tMenu2);
                  }
              }
                t.setTmenu(tMenu1);
              t.setTmenulist(tlist);
              Mlist.add(t);
            }
        }
        return Mlist;
    }

    @Override
    public workbenchView queryworkbenchService() {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        Date now = new Date();
        String time = sdf.format(now);

        workbenchView v=new workbenchView();
        //查询今日预约人数
        v.setOrderpepole( this.tOrderMapper.querydayyuyuershu(time));
        //查询今日评估人数
//        v.setDayassessnum(this.tMentalityResultMapper.selectdaytmentallsum(time));
        //查询今日会员登录次数
        TMemberStatistics querydaylogincnum = this.tMemberStatisticsMapper.querydaylogincnum(time);
        if(querydaylogincnum!=null){
            v.setLoginnum(querydaylogincnum.getLoginnumber());
        }else{
            v.setLoginnum(0);
        }
        //查询今日新增会员
        v.setAddmember(this.tMemberMapper.querydayaddmember(time));
        //查询预约总人数
        v.setOrdersum(this.tOrderMapper.queryuyuesum());
        //查询评估总人数
//        v.setSumassessnum(this.tMentalityResultMapper.selecttmentallsumpeploe());
        //查询会员总数
        v.setMembersum(this.tMemberMapper.querymembersum());
        //查询评估问卷

        //查询干预方案
        v.setScheme(this.tInterposeSchemeMapper.querycountscheme());

        //查询干预追踪
        v.setInterpossfollowup(this.tInterposeFollowupVisitMapper.querycountfollowip());



        //今日评估
        List<TMentalityResult> selectalltmentalu = this.tMentalityResultMapper.selectalltmentalu();
        List<TBodyType> selectalltbody = this.tBodyTypeMapper.selectalltbody();
        v.setDayassessnum(queryTodayPgNumber(selectalltbody,selectalltmentalu));

        //评估总数
        List<String> todats=new ArrayList<String>();
        List<String> todats2=new ArrayList<String>();
        for(TBodyType x:selectalltbody){
            todats.add(x.gettMemberId());
        }
        for(TMentalityResult x:selectalltmentalu){
            todats2.add(x.getMemberId());
        }
        List<String> collect = Stream.of(todats, todats2)
                .flatMap(Collection::stream)
                .distinct()
                .collect(Collectors.toList());
        v.setSumassessnum(collect.size());
        return v;
    }



    public int queryTodayPgNumber(List<TBodyType> l,List<TMentalityResult> l2){
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String data=dateFormat.format(new Date()).toString();
        List<String> todats=new ArrayList<String>();
        List<String> todats2=new ArrayList<String>();
        for (TBodyType p:l
        ) {
            if (p.getCreatetime().contains(data)){
                todats.add(p.gettMemberId());
            }
        }
        for (TMentalityResult p:l2
        ) {
            if(p.getCreateTime()!=null){
                if (p.getCreateTime().contains(data)){
                    todats2.add(p.getMemberId());
                }
            }

        }
        List<String> collect = Stream.of(todats, todats2)
                .flatMap(Collection::stream)
                .distinct()
                .collect(Collectors.toList());
        return collect.size();
    }

}
